/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swa.e_store.businessLogic;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import swa.e_store.DTO.*;
import swa.e_store.ENUM.EmployeeState;
import swa.e_store.entity.Employee;

/**
 *
 * @author Stefan
 */
public  class BL_Authentication {
    
    public Employee authenticate(String username, String password){
        
        //EMFactory
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Napier_E-StorePU");
        
        //EntityManager
        EntityManager eManager = emFactory.createEntityManager();
        
        //Start Transaction
        EntityTransaction tx = eManager.getTransaction();
        tx.begin();
        
        
        Employee employee = (Employee)eManager.createNamedQuery("employee.findByName").setParameter("name", username).getSingleResult();
              
        //PW Check
        if (employee.getKeyword().equals(password)){
            return employee;
        } else{
            //Create unknown Employee-Datafile
            Employee unknownUser = new Employee(9999, username, 0, password);
            return unknownUser;
        }     
    }
    
    
}
